package cn.lvji.springboot.learn2.controller;

import cn.lvji.springboot.learn2.vo.ScenicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 普通controller
 *
 * @author zhouzx
 */
@Controller
class ScenicNormalController implements CommandLineRunner {
    private Map<Long, ScenicVO> map;

    // ‘*’匹配任意字符，‘**’来表示统配任意路径，‘？’匹配单个字符
    @RequestMapping(value = "/scenic/normal/list/*.json", method = RequestMethod.GET)
    @ResponseBody
    public List<ScenicVO> json() {
        List<ScenicVO> list = new ArrayList<>(map.size());
        Set<Map.Entry<Long, ScenicVO>> set = map.entrySet();
        Iterator<Map.Entry<Long, ScenicVO>> it = set.iterator();
        for (Iterator iterator = it; iterator.hasNext(); ) {
            list.add(it.next().getValue());
        }
        return list;
    }

    @RequestMapping(value = "/scenic/normal", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveFormData(ScenicVO scenic) {
        map.put(scenic.getId(), scenic);
        return ResponseEntity.ok().body("ok");
    }

    @RequestMapping(value = "/scenic/norma/json", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResponseEntity<String> saveJson(ScenicVO scenic) {
        map.put(scenic.getId(), scenic);
        return ResponseEntity.ok().body("ok");
    }

    @RequestMapping(value = "/scenic/normal/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ScenicVO get(@PathVariable("id") Long id) {
        return map.get(id);
    }

    @RequestMapping(value = "/scenic/token/{id}", headers = "token=123")
    @ResponseBody
    public ScenicVO getByToken(@PathVariable("id") Long id) {
        return map.get(id);
    }

    @RequestMapping(value = "/scenic", params = "status=2", method = RequestMethod.PATCH)
    @ResponseBody
    public ResponseEntity<String> down(ScenicVO scenic) {
        ScenicVO db = map.get(scenic.getId());
        BeanUtils.copyProperties(scenic, db);
        return ResponseEntity.ok().body("ok");
    }


    /**
     * 启动执行方法
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        map = new HashMap<>(10);
        for (int i = 0; i < 10; i++) {
            ScenicVO scenic;
            if (i % 2 == 0) {
                scenic = new ScenicVO(Long.valueOf(i), "普通景区" + i, 1, "美景", "广东广州");
            } else {
                scenic = new ScenicVO(Long.valueOf(i), "普通景区" + i, 2, "美景", "广东广州");
            }
            map.put(Long.valueOf(i), scenic);
        }
    }
}

